package pers.ree.demo.datetime;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.List;

public class DurationDemo {

    public static void main(String[] args) {
        DurationDemo durationDemo = new DurationDemo();

        durationDemo.plus();

        durationDemo.get();

        durationDemo.between();
    }

    public void plus() {
        Duration duration = Duration.of(60L, ChronoUnit.SECONDS);
        LocalDateTime localDateTime = LocalDateTime.of(2024, 1, 1, 12, 17, 20);
        LocalDateTime plus = localDateTime.plus(duration);
        System.out.println(plus);
    }

    public void get() {
        Duration duration = Duration.of(60L, ChronoUnit.SECONDS);
        long t = duration.get(ChronoUnit.SECONDS);
        System.out.println(t);
        List<TemporalUnit> l = duration.getUnits();
        System.out.println(l);
    }

    public void between() {
        LocalDateTime localDateTime1 = LocalDateTime.of(2024, 1, 1, 8, 17, 20);
        LocalDateTime localDateTime2 = LocalDateTime.of(2024, 2, 1, 20, 17, 20);
        Duration duration = Duration.between(localDateTime1, localDateTime2);
        System.out.println(duration.get(ChronoUnit.SECONDS) + "秒");
        System.out.println(duration.toDays());
        System.out.println(duration.toDaysPart()); // 与toDays的源码一样，功能没有区别
    }
}
